package day008;

public class KnapsackProblem {
    public static void main(String[] args) {

        //n=3 当前背包能装多少个物品
        //w=4 当前背包能装多少重量
        int[] wt = {2, 1, 3};
        int[] val = {4, 2, 3};

        System.out.println(knapsack(3, 4, wt, val));
    }


    /**
     * @param n
     * @param w
     * @param wt
     * @param val
     * @return
     */
    static int knapsack(int n, int w, int[] wt, int[] val) {
        int[][] dp = new int[n + 1][w + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= w; j++) {
                if (j - wt[i - 1] < 0) {
                    System.out.println(j - wt[i - 1]);
                    dp[i][j] = dp[i - 1][j];
                } else {
                    dp[i][j] = Math.max(dp[i - 1][w - wt[i - 1]] + val[i - 1], dp[i - 1][j]);
                }
            }
        }
        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= w; j++) {
                System.out.print(dp[i][j] + "\t");
            }
            System.out.println();
        }
        return dp[n][w];
    }
}
